﻿/********************************************************************************
** @ Date:2025/06/13
** @ Author: Gaojia
** @ Description:
** @ Version:1.0.0
** @ Fun:
********************************************************************************/
#ifndef UDropCellWidget_H
#define UDropCellWidget_H
#include <QWidget>
#include <QMimeData>
#include <QGridLayout>
#include <QVector>
#include "CPageScene.h"
#include "CBaseDevice.h"
#include "CDragPreviewWidget.h"
#include "ui_UDropCellWidget.h"
struct GridItem 
{
	QString sGuid;
	QString sCardGuid;
	QString sDllName;
	int nDeviceType;
	int nRow;
	int nCol;
	int nRowSpan;
	int nColSpan;
	CDragPreviewWidget *pViewWidget;
	GridItem(QString sName,int nType, QString sId,QString sCardId,int Row, int Col, CDragPreviewWidget *pWidgetIn, int RowSpan = 1, int ColSpan = 1)
	{
		sDllName = sName;
		nDeviceType = nType;
		sGuid = sId;
		sCardGuid = sCardId;
		nRow = Row;
		nCol = Col;
		nColSpan = ColSpan;
		nRowSpan = RowSpan;
		pViewWidget = pWidgetIn;
	}
	~GridItem()
	{
		//delete pViewWidget;
	}
};

/**
* @ Author: Gaojia
* @ Description:  拖拽信息结构体
*/
struct SDropInfo
{
	QString sGuid;
	QString sCardGuid;
	QString sDllName;
	int nDeviceType;
	int nIndex;
	int iRow;
	int iCol;
	int rowSpan;
	int colSpan;
	SDropInfo()
	{
		sGuid = "";
		sCardGuid = "";
		sDllName = "";
		nDeviceType = 0;
		nIndex = -1;
		iRow = -1;
		iCol = -1;
		rowSpan = 1;
		colSpan = 1;

	}
};

class UDropCellWidget : public QWidget
{
	Q_OBJECT
public:
	UDropCellWidget(QWidget *parent = nullptr);
	~UDropCellWidget();
	void setRootNode(CBaseNode *pRoot);
	void dragLeaveEvent(QDragLeaveEvent *event);
	void dragEnterEvent(QDragEnterEvent *event) override;
	void dragMoveEvent(QDragMoveEvent *event)override;
	void dropEvent(QDropEvent *event) override;
	void setGridItems(QVector<SPageItem>vecPageItem);
	QVector<SPageItem> getGridItems();
private:
	void addDropWidget(SDropInfo cDropInfo);                     //添加拖拽进来的设备
	void bindDevice(SDropInfo cDropInfo);                        //绑定设备
	void addItem(GridItem *pGridItem);        //添加item
	void removeItem(const GridItem *pGridItem);//删除item
	void updateUsedCells(int row, int col, int rowSpan, int colSpan, bool used);
	void setupContextMenu(QWidget *pWidget);
	CDragPreviewWidget *getCardWidget(QString sGuid, QString sCardGuid, int nIndex);
	CDragPreviewWidget *getCardWidget(QString sDllName, int nDeviceType, QString sCardGuid,int nIndex, QString sGuid="");    //缩略图 通过驱动名称和设备类型去查找
private:
	Ui::UDropCellWidgetClass ui;
	CBaseNode* rootNode;
	QGridLayout *m_pGridLayout;
	int m_iRowCount;
	int m_iColCount;
	QVector<QVector<bool>> m_vvUsedCells;
	QVector<GridItem*> m_vGridItem; 
	QVector<QVector<QLabel*>> m_vecCellWidgets;
	QPoint m_dragStartPos;      // 拖拽起始位置
	QWidget *m_pDragWidget;     // 当前拖动的控件
	GridItem* m_pCurrGridItem;  //当前操作的模块
	SDropInfo m_cDropInfo;      //拖拽进来的时候拖拽信息
private slots:
	void slot_changeCardDevice(QString sGuid);    //修改卡片绑定设备
	void slot_addCardDevice(QString sGuid);       //拖拽进来的时候绑定设备
}; 
#endif